<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-model-item label="模具编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moldNumber">
              <j-search-select-tag placeholder="请输入模具编号" v-model="model.moldNumber" :dictOptions="dictOptions" @change="selectchange()"></j-search-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="次数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="count">
              <j-search-select-tag
                v-model="model.count"
                dict="yl_times"
                placeholder="请输入次数"
              />
              
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerName">
              <a-input v-model="model.customerName" placeholder="请输入客户名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectName">
              <a-input v-model="model.projectName" placeholder="请输入项目名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productName">
              <a-input v-model="model.productName" placeholder="请输入产品名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="产品编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productCode">
              <a-input v-model="model.productCode" placeholder="请输入产品编码" ></a-input>
            </a-form-model-item>
          </a-col>
          
          <a-col :span="12" >
            <a-form-model-item label="模具穴数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="moldCavitiesNum">
              <a-input v-model="model.moldCavitiesNum" placeholder="请输入模具穴数" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="希望完成日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hopeCompleteDate">
              <j-date placeholder="请选择希望完成日期" v-model="model.hopeCompleteDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="设计完成日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="designCompleteDate">
              <j-date placeholder="请选择设计完成日期" v-model="model.designCompleteDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="修正完成日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="correctCompleteDate">
              <j-date placeholder="请选择修正完成日期" v-model="model.correctCompleteDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="计划试模日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="plannedTrialDate">
              <j-date placeholder="请选择计划试模日期" v-model="model.plannedTrialDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="修模指示书子表" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="ylProblemCorrectionChildTable.loading"
          :columns="ylProblemCorrectionChildTable.columns"
          :dataSource="ylProblemCorrectionChildTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'YlProblemCorrectionTableForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
        },
        testres:[],
        dictOptions:[],
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['ylProblemCorrectionChild', ],
        tableKeys:['ylProblemCorrectionChild', ],
        activeKey: 'ylProblemCorrectionChild',
        // 修模指示书子表
        ylProblemCorrectionChildTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '修模主表id',
              key: 'ylProblemCorrectionId',
              type: FormTypes.hidden,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '问题点',
              key: 'problemPoints',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '产生原因',
              key: 'cause',
              type: FormTypes.select,
              dictCode:'yl_cause',
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '责任归属部门',
              key: 'responseDepartment',
              type: FormTypes.select,
              dictCode:'yl_responseDepartment',
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '修正对策',
              key: 'correctiveMeasures',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '责任人',
              key: 'personResponsible',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '问题有无关闭',
              key: 'problemClosed',
              type: FormTypes.select,
              dictCode:'yl_closed',
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '计划完成时间',
              key: 'plannCompleTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '实际完成时间',
              key: 'actCompleTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '修正原因',
              key: 'reason',
              type: FormTypes.select,
              dictCode:'yl_repair_reason',
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/ch/ylProblemCorrectionTable/add",
          edit: "/ch/ylProblemCorrectionTable/edit",
          queryById: "/ch/ylProblemCorrectionTable/queryById",
          queryAllMoldNum: "ch/ylProjectInfo/queryAllMoldNum",
          queryMapByMoldCode:"/ch/ylProblemCorrectionTable/queryMapByMoldCode",
          ylProblemCorrectionChild: {
            list: '/ch/ylProblemCorrectionTable/queryYlProblemCorrectionChildByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      this.queryAllMoldNum();
    },
    methods: {
      queryAllMoldNum(){
        getAction(this.url.queryAllMoldNum).then((res)=>{
              if(res.success){
                this.testres = res.result;
                for(let i = 0;i<this.testres.length;i++){
                  this.dictOptions.push({text:this.testres[i],value:this.testres[i]})
                }
                console.log(this.testres);
              }
            })
      },
      selectchange(){
        getAction(this.url.queryMapByMoldCode,{moldNumber:this.model.moldNumber}).then((res)=>{
          if(res.success){
            console.log(res.result);
            this.moldInformation = res.result;
            this.model.customerName = this.moldInformation.customerName;
            this.model.moldCavitiesNum = this.moldInformation.moldCavitiesNum;
            this.model.productName = this.moldInformation.productName;
            // this.model.productNumber = this.moldInformation.productNumber;
            this.$set(this.model,'productCode',this.moldInformation.productNumber);

          }
        })
      },
      addBefore(){
        this.ylProblemCorrectionChildTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.ylProblemCorrectionChild.list, params, this.ylProblemCorrectionChildTable)
        }
      },
      //校验所有一对一子表表单
      validateSubForm(allValues){
          return new Promise((resolve,reject)=>{
            Promise.all([
            ]).then(() => {
              resolve(allValues)
            }).catch(e => {
              if (e.error === VALIDATE_NO_PASSED) {
                // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
              } else {
                console.error(e)
              }
            })
          })
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          ylProblemCorrectionChildList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>